﻿using UnityEngine;
using System.Collections;

public class NameTag : MonoBehaviour {

    [HideInInspector]
    public GameObject m_pOwner = null;

    public string m_strName;
    float m_fDefalutOffset_y;

    GameObject MainCam;

	// Use this for initialization
	void Start () {
        m_fDefalutOffset_y = transform.GetChild(0).localPosition.y;
        transform.localScale = Vector3.one;

        GetComponent<UIFollowTarget>().target = m_pOwner.transform;
        GetComponent<UIFollowTarget>().gameCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
        GetComponent<UIFollowTarget>().uiCamera = GameObject.Find("Camera").GetComponent<Camera>();
        GetComponent<UIFollowTarget>().enabled = true;

        RefreshName();

        MainCam = GameObject.Find("MainCamera").gameObject;
	}

    void Update()
    {
        transform.GetChild(0).localPosition = new Vector3(0, m_fDefalutOffset_y - ((1.5f - MainCam.GetComponent<Camera>().orthographicSize) * 30.0f));
        transform.GetChild(1).localPosition = new Vector3(0, m_fDefalutOffset_y - ((1.5f - MainCam.GetComponent<Camera>().orthographicSize) * 30.0f));
    }

    public void RefreshName()
    {
        if (m_pOwner.GetComponent<UnitStatus>().m_unitData.strNickName == "")
        {
            m_strName = m_pOwner.GetComponent<UnitStatus>().m_unitData.strLastName;
            transform.FindChild("Label").GetComponent<UILabel>().text = m_strName;
        }
        else
        {
            m_strName = m_pOwner.GetComponent<UnitStatus>().m_unitData.strNickName;
            transform.FindChild("Label").GetComponent<UILabel>().text = m_strName;
        }

        transform.Find("Container").GetComponent<UISprite>().SetDimensions((int)(transform.Find("Label").GetComponent<UILabel>().width * 0.15f), 25);
    }
}
